<?php
namespace app\common\controller;

use app\common\controller\Base;
use think\Request;
use think\Config;
use think\Session;

class Seller extends Base
{
//    use \app\common\traits\Curd;
    
    protected $modelClass=null;
    protected $logicClass = null;
    protected $serviceClass = null;
    protected $authenticate;

    
    protected function _initialize(){
    
        //定义model
        if ($this->modelClass == null) {
            
            $modelName = str_replace("Controller","",Request::instance()->controller());
            $this->modelClass = 'app\common\model\\'.ucfirst(preg_replace('/[^>]+\./','',$modelName));//默认model
            $this->logicClass = 'app\common\logic\\'.ucfirst(preg_replace('/[^>]+\./','',$modelName)).'Logic';//默认model
        }
        
        //指定哪些方法需要登陆，后面要替换掉auth
        if ($this->authenticate) {
            $this->beforeActionList['authenticate'] = $this->authenticate;
        } else {
            $this->beforeActionList['authenticate'] = null;//为null的话默认所有接口都要权限
        }
    
        $member = session('ke_seller_auth');
        $this->assign('co_name',$member['co_name']);
        
        $this->assign('menu', config('menu'));//后台菜单
  
        
    }
    
    /**
     *  鉴定身份
     */
    protected function authenticate()
    {
        
        define('SID', is_login());
        if (!SID) {
            set_redirect_url($_SERVER['REQUEST_URI']);
            $this->error('需要登录','/user/login');
        }
        
    }
    
    /**
     * @desc 登录的时候用的是id
     * @return mixed
     */
    protected function getSellerId(){
        $member = session('ke_seller_auth');
        if(!isset($member)||!isset($member['id'])){
            
            $this->error('商户信息丢失,请重新登录');
        }
        return $member['id'];
    }
    
    
    /**
     * 模板变量赋值
     */
    protected function setAssign($signArr = [])
    {
        
        foreach ($signArr as $k => $v) {
            $this->assign($k, $v);
        }
    }
    
    
    /**
     * 查- 列表
     * @return string
     */
//    public function index()
//    {
//
//        $list = $this->prepareDataProvider();
//        $p = $list->toArray();
//        $this->assign('total', $p['total']);
//        $this->assign('perPage', $p['per_page']);
//        $this->assign('currentPage', $p['current_page']);
//        $this->assign('list', $list);
//        return $this->fetch();
//    }

    /**
     * 列表
     */
    public function index()
    {
        $params = paramForm();
        
        //如果有id参数，则表示获取单个
        if(isset($params['id'])){
            $this->read($params);
            die;
        }
        
        if(Request::instance()->isAjax()){
            $logicClass = $this->logicClass;
            $logic = new $logicClass();
            $lists = $logic->getList($params);
            $list = $logic->prepareDataProvider();
            success($list);
        }else{
            return $this->fetch();
        }
       
    }
    
    public function read($params)
    {
        $logicClass = $this->logicClass;
        $logic = new $logicClass();
        
        $logic->setSql($params);
        
        $one = $logic->findModel();
    
        if(Request::instance()->isAjax()){
            if($one){
                success($one);
            }else{
                error('数据不存在或者无权限查看');
            }
        }else{
    
            if($one){
                $this->assign('x',$one);
                return $this->fetch();
            }else{
                $this->error('数据不存在或者无权限查看');
            }
            
            
        }
      
    }
    
    
    /**
     * 增- 表单
     * @return string
     */
    public function add()
    {
        return $this->fetch();
    }
    
    
    /**
     * 增 - 确认保存
     * @return string
     */
    public function save()
    {
        $logicClass = $this->logicClass;
        $logic = new $logicClass();
        $rt = $logic->add(paramForm());
        
        if ($rt) {
            $this->success("新增成功！", 'index');
        } else {
            $this->error("新增失败！");
        }
        
        
    }
    
    /**
     * 改 - 表单
     * @param $id
     * @return string
     */
    public function edit($id)
    {
        $logicClass = $this->logicClass;
        $logic = new $logicClass();
        $model = $logic->findModel($id);
        $this->assign('model', $model);
        return $this->fetch();
    }
    
    /**
     * 改 - 确认修改
     * @param $id
     * @return string
     */
    public function update($id)
    {
        $logicClass = $this->logicClass;
        $logic = new $logicClass();
        $model = $logic->findModel($id);
        $rt = $model->allowField(true)->save(paramForm(),['id'=>$id]);
    
        if(Request::instance()->isAjax()){
            if($rt){
                api(1,'更新成功',null);
            }else{
                error('更新失败,您可能没有没有修改任何数据');
            }
        }else{
        
            if($rt){
                $this->success("更新成功！", "index");
            }else{
                $this->error("更新失败!");
            }
        
        
        }
        
    }
    
    /**
     * 删除 - 不是真的删除，而是修改状态而已。
     * @param $id
     * @return string
     */
//    public function delete($id)
//    {
//        $model = $this->findModel($id);
//        if(!isset($model->flag)){
//            $this->error('该表没有flag字段');
//        }
//        $params = ['flag'=>0];
//        $rt = $model->allowField(true)->save($params,['id'=>$id]);
//
//        if ($rt) {
//            $this->success("删除成功！");
//        } else {
//            $this->error("删除失败!");
//        }
//    }
    
    
    /**
     * 删，这是真的删了。。。
     * @param $id
     * @return string
     */
    public function delete($id)
    {
        $logicClass = $this->logicClass;
        $logic = new $logicClass();
        $model = $logic->findModel($id);
        $rt = $model->delete();
        if ($rt) {
            $this->success("删除成功！", "index");
        } else {
            $this->error("删除失败！");
        }
        
    }
    
   
    
    /**
     * 重写 表单验证
     */
    protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
    {
        $result = parent::validate($data, $validate, $message, $batch, $callback); // TODO: Change the autogenerated stub
        if (true !== $result) {
            $this->error($result);
        }
    }
    
    protected function setPost($params,$filed=''){
        $arr = [];
        foreach ($params as $key=>$value){
            if(strpos($filed,$key)>-1){
                $arr[$key] = $value;
            }
        }
        return $arr;
    }
    
    
}
